package com.wmx.thread.synchronize_and_lock.condition;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**********************************
 * @author wmx
 * @date 2019/9/19 15:57
 * @description:
 * @version 1.0
 *
 ************************************/
public class ConditionDemo {
    private static ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) throws InterruptedException {
        Condition condition = lock.newCondition();

        Thread thread = new Thread(() -> {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"获得锁");
                condition.await();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"释放锁");
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"获得锁");
                condition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"释放锁");
            }
        });
        thread.start();
        thread2.start();


    }
}
